/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.form.ClassValidationForm;
import vn.webapp.model.Classes;
import vn.webapp.service.ClassesService;
/**
 *
 * @author Tonytran
 */
@Controller("cpClasses")
@RequestMapping(value = {"/cp"})
public class ClassesController extends BaseWeb{
    private int nav = 1;
    @Autowired
    private ClassesService classesService;
    
    @Autowired(required = true)
    @Qualifier(value = "classesService")
    public void setClassesService(ClassesService classesService) {
        this.classesService = classesService;
    }
    /**
     * 
     * @param model
     * @return 
     */
    @RequestMapping(value = "/classes" , method = RequestMethod.GET)
    public String classes(ModelMap model,
        HttpSession session,
        @RequestParam(value = "page", defaultValue = "0") int page){
        int pageSize = 10;        
        DataPage<Classes> dataClasses = classesService.filter(page, pageSize);
        model.put("sz_Response", session.getAttribute("response"));
        session.setAttribute("response", "");
        model.put("dataClasses", dataClasses);
        model.put("pageIndex", page);
        model.put("nav", this.nav);        
        return "cp.classes";
    }    
   
    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/classdetail/{id}")
    public String viewDetail(ModelMap model, @PathVariable("id") String id) {
        Classes _class = classesService.viewDetail(id);
        model.put("classForm", new ClassValidationForm());
        model.put("dataClass", _class);
        model.put("nav", this.nav);
        return "cp.classesdetail";
    }
    
    /**
     *
     * @param classForm
     * @param result
     * @param request
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/saveclassdetail", method = RequestMethod.POST)
    public String processEditClass(@Valid @ModelAttribute("classForm") ClassValidationForm classForm,
            BindingResult result,HttpServletRequest request, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.classesdetail";
        }else
        {
            String ClassCode = classForm.getCode();
            String ClassId = classForm.getId();
            Classes o_Class = classesService.checkExistsCode(ClassCode, ClassId);
            if(o_Class != null)
            {
                model.put("sz_Response", "The code "+ClassCode+" is being used.");
                return "cp.classesdetail";
            }else{
                int i_InsertUser = classesService.updateTheClass(ClassCode, ClassId, classForm.getName());
                session.setAttribute("response", "Edit successfully!");
                return "redirect:" + this.baseUrl + "/cp/classes.html";
            }
        }
    }
    
    @RequestMapping(value = "/removeclass/{id}", method = RequestMethod.GET)
    public String processRemoveClass(ModelMap model, @PathVariable("id") String id, HttpSession session) {
        int i_IsRemoved = classesService.removeClass(id);
        String sz_Response;
        if (i_IsRemoved != 0) {
            sz_Response = "Remove the class successfully !";
        } else {
            sz_Response = "Remove the class not successfully. Please retry";
        }
        session.setAttribute("response", sz_Response);
        return "redirect:" + this.baseUrl + "/cp/classes.html";
    }
    
    /**
     *
     * @param model
     * @param result
     * @return
     */
    @RequestMapping(value = "/addnewclass", method = RequestMethod.GET)
    public String addnewclass(Map model){
        model.put("nav", this.nav);
        model.put("classFormAdd", new ClassValidationForm());
        return "cp.addnewclass";
    }
    
    /**
     * 
     * @param userForm
     * @param result
     * @param model
     * @param session
     * @return 
     */
    @RequestMapping(value = "/saveclass", method = RequestMethod.POST)
    public String processAddNewClass(@Valid @ModelAttribute("classFormAdd") ClassValidationForm classFormAdd,
            BindingResult result, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.addnewclass";
        }else
        {
            String ClassCode = classFormAdd.getCode();
            String ClassId = classFormAdd.getId();
            String ClassName = classFormAdd.getName();
            Classes o_Class = classesService.checkExistsCode(ClassCode, ClassId);
            if(o_Class != null)
            {
                model.put("sz_Response", "The code "+ClassCode+" is being used.");
                return "cp.addnewclass";
            }else
            {
                int i_InsertUser = classesService.saveAClass(ClassCode, ClassName);
                session.setAttribute("response", "Add successfully!");
                return "redirect:" + this.baseUrl + "/cp/classes.html";
            }
        }
    }
}
